package com.otdemo.dao;

import com.mysql.cj.util.StringUtils;
import com.otdemo.entity.RestScore;
import com.otdemo.util.CacheUtil;
import com.otdemo.util.DBUtils;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class RestScoreDao {

    public void insert(RestScore order) {
        try {
            Connection connection = new DBUtils().getConnection();
            String sql = "insert into stu_score\n" +
                    "(uid, content, `user`, score, classification, deleted, createDate, updateDate, operator) \n" +
                    "values (?, ?, ?, ?, ?, 0, now(), now(), ?)";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, UUID.randomUUID().toString());
            statement.setString(2, order.getContent());
            statement.setString(3, order.getUser());
            statement.setBigDecimal(4, order.getScore());
            statement.setString(5, order.getClassification());
            statement.setString(6, order.getOperator());
            statement.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void update(RestScore order) {
        try {
            Connection connection = new DBUtils().getConnection();
            String sql = "update stu_score set \n" +
                    "content       = ?,\n" +
                    "user     = ?,\n" +
                    "score     = ?,\n" +
                    "classification     = ?,\n" +
                    "deleted    = ?,\n" +
                    "updateDate = now(),\n" +
                    "operator   = ?\n" +
                    "where uid = ?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, order.getContent());
            statement.setString(2, order.getUser());
            statement.setBigDecimal(3, order.getScore());
            statement.setString(4, order.getClassification());
            statement.setBoolean(5, order.getDeleted());
            statement.setString(6, order.getOperator());
            statement.setString(7, order.getUid());
            statement.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public RestScore findByUid(String queryUid) {
        try {
            Connection connection = new DBUtils().getConnection();
            String sql = "select id, uid, content, `user`, score, classification, deleted, createDate, updateDate, operator " +
                    "from stu_score where uid = ? and deleted = 0 ";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, queryUid);
            ResultSet rs = statement.executeQuery();
            // 展开结果集数据库
            while (rs.next()) {
                // 通过字段检索
                Integer id = rs.getInt("id");
                String uid = rs.getString("uid");
                String content = rs.getString("content");
                String user = rs.getString("user");
                BigDecimal score = rs.getBigDecimal("score");
                String classification = rs.getString("classification");
                Boolean deleted = rs.getBoolean("deleted");
                Date createDate = rs.getDate("createDate");
                Date updateDate = rs.getDate("updateDate");
                String operator = rs.getString("operator");
                RestScore order = new RestScore();
                order.setId(id);
                order.setUid(uid);
                order.setContent(content);
                order.setUser(user);
                order.setScore(score);
                order.setClassification(classification);
                order.setDeleted(deleted);
                order.setCreateDate(createDate);
                order.setUpdateDate(updateDate);
                order.setOperator(operator);
                return order;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    public List<RestScore> list() {
        List<RestScore> orderList = new ArrayList<>();
        String name1 = CacheUtil.getString(CacheUtil.USER_SEARCH_NAME);
        ResultSet rs;
        try {
            if(StringUtils.isNullOrEmpty(name1)){
                Connection connection = new DBUtils().getConnection();
                String sql = "select id, uid, content, `user`, score, classification, deleted, createDate, updateDate, operator " +
                        "from stu_score where deleted = 0 ";
                PreparedStatement statement = connection.prepareStatement(sql);
                rs = statement.executeQuery();
            }else {
                Connection connection = new DBUtils().getConnection();
                String sql = "select id, uid, content, `user`, score, classification, deleted, createDate, updateDate, operator " +
                        "from stu_score where deleted = 0 and user like ? ";
                PreparedStatement statement = connection.prepareStatement(sql);
                statement.setString(1, "%" + name1 + "%");
                rs = statement.executeQuery();
            }
            // 展开结果集数据库
            while (rs.next()) {
                // 通过字段检索
                Integer id = rs.getInt("id");
                String uid = rs.getString("uid");
                String content = rs.getString("content");
                String user = rs.getString("user");
                BigDecimal score = rs.getBigDecimal("score");
                String classification = rs.getString("classification");
                Boolean deleted = rs.getBoolean("deleted");
                Date createDate = rs.getDate("createDate");
                Date updateDate = rs.getDate("updateDate");
                String operator = rs.getString("operator");
                RestScore order = new RestScore();
                order.setId(id);
                order.setUid(uid);
                order.setContent(content);
                order.setUser(user);
                order.setScore(score);
                order.setClassification(classification);
                order.setDeleted(deleted);
                order.setCreateDate(createDate);
                order.setUpdateDate(updateDate);
                order.setOperator(operator);
                orderList.add(order);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return orderList;
    }

}
